package com.yao.nettydemo.base;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * Created with IntelliJ IDEA.
 * Description: 客户端事件处理类
 * User: admin
 * Date: 2018-11-01
 * Time: 19:28
 */
public class ClientHandler extends SimpleChannelInboundHandler<String> {

    /**
     * 连接上时触发
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //发送上线消息
        ctx.writeAndFlush(Unpooled.copiedBuffer("我来啦".getBytes()));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("我错了" + cause.getMessage());
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println("client receive :" +msg);
    }
}
